package com.dofast.bt.mvp.presenter.basket;

import com.dofast.bt.adapter.GoodsAdapter;
import com.dofast.bt.base.BaseMvpPresenter;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.bean.ShopBean;
import com.dofast.bt.mvp.model.IMainModel;
import com.dofast.bt.mvp.model.MainModel;
import com.dofast.bt.mvp.view.activity.basket.StoreDetailView;
import com.dofast.bt.util.Helper;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/20.
 */

public class StoreDetialPresenter extends BaseMvpPresenter<StoreDetailView> implements IStoreDetialPresenter {
    private GoodsAdapter adapter = null;
    private List<GoodsBean> list = null;
    private IMainModel mainModel = null;

    public StoreDetialPresenter() {
        mainModel = new MainModel();
    }

    @Override
    public void initData() {
        checkViewAttach();
        try {
            ShopBean shopBean = mainModel.getShopById(mvpView.getShopId());
            if (null == shopBean) {
                return;
            }
            mvpView.initViewData(shopBean, mainModel.isAttention(mvpView.getShopId()));
            if (shopBean.getGoodses() != null) {
                list = shopBean.getGoodses();
            } else {
                list = new ArrayList<>();
            }
            adapter = new GoodsAdapter(list);
            mvpView.initAdapter(adapter);
            adapter.notifyDataSetChanged();
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void itemClick(int position) {
        mvpView.gotoTeaDetailActivity(list.get(position));
    }

    @Override
    public void attention(int shopId) {
        try {
            if (mainModel.attentionShop(shopId)) {
                Helper.showToast("关注成功");
                initData();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    private void getGoodsData() {
        for (int i = 0; i < 9; i++) {
            GoodsBean good = new GoodsBean();
            good.setName("铁观音");
            good.setPrice(165.00 + i);
            list.add(good);

        }
    }
}